// UVA 101



#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int n, cord[2];
string comd, word;
vector<vector<int> > v(25, vector<int>());
vector<int> temp;

void search(int a, int b) {
    int cnt = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < (int)v[i].size(); j++) {
            if(2 == cnt) {
                break;
            }
            if(v[i][j] == a) {
                cord[0] = i;
                cnt++;
            }
            if(v[i][j] == b) {
                cord[1] = i;
                cnt++;
            }
        }
    }
    return;
}

void returning(int b, int i) {
    while(v[i].back() != b) {
        int end = v[i].back();      
        v[i].pop_back();
        v[end].push_back(end);
    }
    
    return;
}

void move_onto(int a, int b) {
    search(a, b);
    if(cord[0] == cord[1]) {
        return;
    }

    returning(a, cord[0]);
    returning(b, cord[1]);

    v[cord[0]].pop_back();
    v[cord[1]].push_back(a);

    return;
}

void move_over(int a, int b) {
    search(a, b);
    if(cord[0] == cord[1]) {
        return;
    }

    returning(a, cord[0]);

    v[cord[0]].pop_back();
    v[cord[1]].push_back(a);

    return;
}

void pile(int a) {
    while(v[cord[0]].back() != a) {
        temp.push_back(v[cord[0]].back());
        v[cord[0]].pop_back();
    }
    temp.push_back(a);
    v[cord[0]].pop_back();    

    for(int i = temp.size()-1; i >= 0; i--) {
        v[cord[1]].push_back(temp[i]);
    }
}

void pile_over(int a, int b) {
    search(a, b);
    if(cord[0] == cord[1]) {
        return;
    }

    pile(a);

    return;
}

void pile_onto(int a, int b) {
    search(a, b);
    if(cord[0] == cord[1]) {
        return;
    }

    returning(b, cord[1]);

    pile(a);

    return;
}

int main() {
    cin >> n;
    
    for(int i = 0; i < n; i++) {
        v[i].push_back(i);
    }
    cin.get();

    while(1) {
        getline(cin, comd);

        if(!comd.compare("quit")) {
            break;
        }

        word = comd.substr(0, 4) + comd.substr(7, 4);
                
        if(!word.compare("moveonto")) {
            move_onto(comd[5]-'0', comd[12]-'0');
        } else if(!word.compare("moveover")) {
            move_over(comd[5]-'0', comd[12]-'0');
        } else if(!word.compare("pileover")) {
            pile_over(comd[5]-'0', comd[12]-'0');
        } else if(!word.compare("pileonto")) {
            pile_onto(comd[5]-'0', comd[12]-'0');
        }
    }

    for(int i = 0; i < n; i++) {
        cout << i << ": ";
        for(unsigned int j = 0; j < v[i].size(); j++) {
            cout << v[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}




// answer
#include <iostream>
#include <vector>
#include <string>

using namesapce std;

int n;
vector<int> bk[30];


void init() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        bk[i].push_back(i);
    }
}

void loc(int x, int& p, int& h) { //引用，只用于c++
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < bk[i].size(); j++) {
            if(bk[i][j] == x) {
                p = i;
                h = j;
                break;
            }
        }
    }
}

//归位
void goback(int p, int h) {
    for(int i = h+1; i < bk[p].size(); i++) {
        int k = bk[p][i];
        bk[k].push_back(k);
    }
    //一定要重置大小
    bk[p].resize(h+1);
}


void all_move(int p, int h, int q) {
    for(int i = h; i < bk[p].size(); i++) {
        bk[q].push_back(bk[p][i]);
    }
    bk[p].resize(h);
}

void solve() {
    int a, b;
    string s1, s2;

    while(cin >> s1) {  //相当于死循环
        if(s1 == "quit") {
            break;
        }

        cin >> a >> s2 >> b;
        int ap = 0, ah = 0;  //a的坐标
        int bp = 0, bh = 0;

        loc(a, ap, ah);  //寻找a的坐标
        loc(b, bp, bh);

        if(ap == bp) {
            continue;
        }

        if(s1 == "move") {  //a上归位
            goback(ap, ah);
        }
        if(s2 == "onto") { //b上归位
            goback(bp, bh);
        }

        //所有都要移动
        all_move(ap, ah, bp);
    }
}

void print() {
    for(int i = 0; i < n; i++) {
        cout << i << ":";
        for(int j = 0; j < bk[i].size(); j++) {
            cout << " " << bk[i][j];
        }
        cout << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false); //优化cin和cout的速度
    
    init();
    solve();
    print();

    return 0;
}


